package org.example;

import org.example.builder.RaceCarBuilder;
import org.example.entity.RaceCar;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        Player player = new Player();
        player.setRb(getRaceCarBuilder());
        RaceCar myRaceCar = player.construct();
    }

    static RaceCarBuilder getRaceCarBuilder() {
        try {
            //1.get builder name
            String filePath = "BuilderPattern\\src\\main\\resources\\builder.properties";
            Properties properties = new Properties();
            properties.load(new FileReader(filePath));
            String builder = properties.getProperty("builder");
            System.out.println("类名："+ builder);

            //2.get builder bean
            Class c = Class.forName(builder);
            Constructor constructor = c.getConstructor(null);
            return (RaceCarBuilder) constructor.newInstance();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}